package com.mycompany.rest;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.View;

import com.mycompany.optimizaproject.Cliente;
import com.mycompany.optimizaproject.Incidente;
import com.mycompany.optimizaproject.Usuario;
import com.mycompany.services.dao.ClienteDAO;
import com.mycompany.services.dao.IncidenteDAO;
import com.mycompany.services.dao.UsuarioDAO;

@Controller
@RequestMapping("/rest")
public class FundRestController {
	
	private static final Logger log = Logger.getLogger(FundRestController.class);
	
	private static final String DATA_FIELD = "data";
	private static final String ERROR_FIELD = "error";
	
	@Autowired
	private View jsonView_i;
	
	@Autowired
	private ClienteDAO client;
	
	@Autowired
	private UsuarioDAO usuario;
	
	@Autowired
	private IncidenteDAO incidente;
	
	//Obtengo todos los clientes
	@RequestMapping(value = "/clientes/", method = RequestMethod.GET)  
    public ModelAndView getClients() {  
            List<Cliente> clients = null;  
    
            try {  
                 clients = client.getList();  
            } catch (Exception e) {  
                 String sMessage = "Error getting clients. [%1$s]"; 
                 e.printStackTrace();
                 return createErrorResponse(String.format(sMessage, e.toString()));  
            }  
    
            log.debug("Returing Funds: " + clients.toString());  
            return new ModelAndView(jsonView_i, "clients", clients);  
       }  

	//Obtengo todos los incidentes
	@RequestMapping(value = "/incidentes/", method = RequestMethod.GET)  
    public ModelAndView getIncidentes() {  
            List<Incidente> incidentes = null;  
    
            try {  
            	incidentes = this.incidente.getList();  
            } catch (Exception e) {  
                 String sMessage = "Error obteniendo incidentes. [%1$s]"; 
                 e.printStackTrace();
                 return createErrorResponse(String.format(sMessage, e.toString()));  
            }  
    
            log.debug("Returing incidentes: " + incidentes.toString());  
            return new ModelAndView(jsonView_i, "incidentes", incidentes);  
       } 
	
	//Obtengo todos los incidentes del usuario con el id pasado por parametro
	//urlEj:/optimizaAppWeb/rest/incidentes/1001
	@RequestMapping(value = "/incidentes/usuario/{userId}", method = RequestMethod.GET)  
    public ModelAndView getIncidentesByUser(@PathVariable long userId) {  
            List<Incidente> incidentes = null;  
    
            try {  
            	//incidentes = this.incidente.getIncidentsByUser(userId);  
            } catch (Exception e) {  
                 String sMessage = "Error obteniendo incidentes. [%1$s]"; 
                 e.printStackTrace();
                 return createErrorResponse(String.format(sMessage, e.toString()));  
            }  
    
            log.debug("Returing incidentes: " + incidentes.toString());  
            return new ModelAndView(jsonView_i, "incidentes de usuario " + userId, incidentes);  
       }
	
	//Obtengo el incidente con el id pasado por parametro
	//urlEj:/optimizaAppWeb/rest/incidentes/1
	@RequestMapping(value = "/incidentes/{idIncidente}", method = RequestMethod.GET)  
    public ModelAndView getIncidentesById(@PathVariable long idIncidente) {  
            List<Incidente> incidentes = null;  
    
            try {  
            	incidentes = this.incidente.getIncidentsById(idIncidente);  
            } catch (Exception e) {  
                 String sMessage = "Error obteniendo incidentes. [%1$s]"; 
                 e.printStackTrace();
                 return createErrorResponse(String.format(sMessage, e.toString()));  
            }  
    
            log.debug("Returing incidentes: " + incidentes.toString());  
            return new ModelAndView(jsonView_i, "incidente " + idIncidente, incidentes);  
       } 
	
	//Obtengo el incidente con el id pasado por parametro
		//urlEj:/optimizaAppWeb/rest/incidentes/1
		@RequestMapping(value = "/incidentes/cliente/{idCliente}", method = RequestMethod.GET)  
	    public ModelAndView getIncidentesByClient(@PathVariable long idCliente) {  
	            List<Incidente> incidentes = null;  
	    
	            try {  
	            	//incidentes = this.incidente.getIncidentsByClient(idCliente);  
	            } catch (Exception e) {  
	                 String sMessage = "Error obteniendo incidentes. [%1$s]"; 
	                 e.printStackTrace();
	                 return createErrorResponse(String.format(sMessage, e.toString()));  
	            }  
	    
	            log.debug("Returing incidentes: " + incidentes.toString());  
	            return new ModelAndView(jsonView_i, "incidente para el cliente " + idCliente, incidentes);  
	       } 
	
	@RequestMapping(value = "/usuarios/", method = RequestMethod.GET)  
    public ModelAndView getUsuarios() {  
            List<Usuario> usuarios = null;  
    
            try {  
            	usuarios = this.usuario.getList();  
            } catch (Exception e) {  
                 String sMessage = "Error obteniendo usuarios. [%1$s]"; 
                 e.printStackTrace();
                 return createErrorResponse(String.format(sMessage, e.toString()));  
            }  
    
            log.debug("Returing usuarios: " + usuarios.toString());  
            return new ModelAndView(jsonView_i, "usuarios", usuarios);  
       }
	
	@RequestMapping(value = "/usuarios/login", method = RequestMethod.GET, params={"name", "password"})  
    public ModelAndView loginUser(@RequestParam(value="name")String name, @RequestParam(value="password")String password) {  
            List<Usuario> usuarios = null;  
    
            try {  
            	usuarios = this.usuario.loginUser(name, password);  
            } catch (Exception e) {  
                 String sMessage = "Error obteniendo usuario. [%1$s]"; 
                 e.printStackTrace();
                 return createErrorResponse(String.format(sMessage, e.toString()));  
            }  
    
            log.debug("Returing usuarios: " + usuarios.toString());  
            return new ModelAndView(jsonView_i, "usuario", usuarios);  
       }
	/**|
	 * Create an error REST response.
	 *
	 * @param sMessage
	 *            the s message
	 * @return the model and view
	 */
	private ModelAndView createErrorResponse(String sMessage) {
		return new ModelAndView(jsonView_i, ERROR_FIELD, sMessage);
	}

	/**
	 * Injector methods.
	 *
	 * @param view
	 *            the new json view
	 */
	public void setJsonView(View view) {
		jsonView_i = view;
	}
}
